package edu.pdx.cs.guavadata.domain;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import java.lang.String;
import java.sql.ResultSet;


/**
 *
 * @author VuTran
 */
public class TinyInteger extends Domain {
    private Byte min;
    private Byte max;
    
    public Byte getMax() {
        return max;
    }
    
    public void setMax(Byte value) {
        max = value;
    }
    
    public Byte getMin() {
        return min;
    }
    
    public void setMin(Byte value) {
        min = value;
    }

    public TinyInteger() {
        this.min = null;
        this.max = null;
    }

    public TinyInteger(Byte min, Byte max) {
        this.min = min;
        this.max = max;
    }

    @Override
    public boolean validate(Object o) {
        Byte i = null;
        
        if (o == null) return true;
        if (o instanceof java.lang.Byte) i = (Byte) o;
        
        // If still null at this point, then o isn't a valid byte-worthy object
        if (i == null) return false;
        if ((min != null) && (min > i)) return false;
        if ((max != null) && (max < i)) return false;
        return true;
    }

    @Override
    public Object convertString(String s) throws DomainConversionException {
        if ((s == null) || (s == "")) return null;
        Byte d;
        try {
            d = Byte.parseByte(s);
        } catch (NumberFormatException e) {
            throw new DomainConversionException("Could not convert " + s + " into a 8-bit integer.");
        }
        
        if ((min != null) && (min > d)) throw new DomainConversionException("Value " + s + " is too small for a domain with minimum value " + min.toString() + ".");
        if ((max != null) && (max < d)) throw new DomainConversionException("Value " + s + " is too big for a domain with minimum value " + max.toString() + ".");
        return d;
    }

    @Override
    public Class getNaiveType() {
        return Byte.class;
    }

    @Override
    public String getDBType() {
        return "tinyint";
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof TinyInteger)) return false;
        TinyInteger that = (TinyInteger) obj;
        return ((this.max == that.max) && (this.min == that.min));
    }

    @Override
    public int hashCode() {
        return TinyInteger.class.hashCode() * (min.hashCode() + 1) + max.hashCode();
    }

    @Override
    public String toString() {
        return "TinyInteger";
    }
    @Override
    public Domain upcast() {
        return new SmallInteger(min.shortValue(), max.shortValue());
    }

    @Override
    public Object upcastValue(Object o) {
        if (o == null) return null;
        if (o instanceof Byte) return (Short) o;
        return null;
    }

    @Override
    public Domain merge(Domain d) {
        if (!(d instanceof TinyInteger)) return null;
        TinyInteger that = (TinyInteger) d;
        Byte nmax = (this.max == that.max) ? this.max : null;
        Byte nmin = (this.min == that.min) ? this.min : null;
        return new TinyInteger(nmin, nmax);
    }

    @Override
    public boolean isOrdered() {
        return true;
    }

    @Override
    public boolean canContain() {
        return false;
    }

    @Override
    public Domain clone() {
        return new TinyInteger(this.min, this.max);
    }

    @Override
    public Element ToXml(Document xdoc) {
        Element xe = xdoc.createElement("Domain");
        xe.setAttribute("Type", "TinyInteger");
        if (max != null) xe.setAttribute("MaxValue", max.toString());
        if (min != null) xe.setAttribute("MinValue", min.toString());
        return xe;
    }

    public static TinyInteger fromElement(Element e) {
        TinyInteger o = new TinyInteger();
        if (e.hasAttribute("MaxValue")) {
            o.max = Byte.parseByte(e.getAttribute("MaxValue"));
        }
        if (e.hasAttribute("MinValue")) {
            o.min = Byte.parseByte(e.getAttribute("MinValue"));
        }
        return o;
    }
    
    public static TinyInteger FromDBInfoRow(ResultSet dr) {
        TinyInteger o = new TinyInteger();
        return o;
    }
}
